跳到主要内容

ES 查询与运维命令速查

本篇把日常最常用的请求收在一起,按「集群与节点 / _cat / 索引与文档 / 查询与聚合」分表。默认服务地址是 http://localhost:9200,示例里的索引名按实际替换。

通用约定​

  • 方法语义:GET 查、PUT 建(幂等)、POST 写/执行、DELETE 删、HEAD 探活。
  • 加 ?pretty:返回带缩进的 JSON,人看方便,脚本里别加。
  • 请求体一律带 header:-H 'Content-Type: application/json',否则 8.x 会拒绝解析。
  • NDJSON 接口(_bulk、_msearch)用 Content-Type: application/x-ndjson,每行一个 JSON,末尾必须有换行。
  • filter_path 裁剪返回:?filter_path=status,number_of_nodes,脚本里取值更省事。
  • ?human 让大小、时间带单位(1.2gb、3.4s),?bytes=gb 指定单位。

集群与节点​

方法路径说明
GET/_cluster/health集群三色、节点数、分片数
GET/_cluster/health?level=indices带上每个索引的健康明细
GET/_cluster/health?wait_for_status=yellow&timeout=30s阻塞等待集群达到指定状态
GET/_cluster/stats集群级统计:文档数、存储、内存
GET/_cluster/state集群状态元数据(很大,慎用)
GET/_cluster/settings?include_defaults=true当前生效的集群设置
PUT/_cluster/settings修改动态集群设置
GET/_cluster/allocation/explain解释分片为什么没分配
POST/_cluster/reroute?retry_failed=true重试失败的分片分配
GET/_nodes节点列表与基本信息
GET/_nodes/stats节点全量指标(JVM、OS、FS、索引)
GET/_nodes/stats/jvm,os,fs只取关心的几类指标
GET/_nodes/hot_threads当前最忙的线程栈,卡顿排查首选
GET/_tasks正在跑的任务,找失控查询
POST/_tasks/节点ID:任务ID/_cancel取消指定任务
GET/_snapshot/仓库名/_all快照列表与状态
POST/_snapshot/仓库名/snap名/_restore从快照恢复
GET/_slm/policy快照生命周期策略
GET/{index}/_ilm/explain索引的 ILM 执行状态

建仓库、建快照、恢复​

PUT /_snapshot/fs_backup
{
"type": "fs",
"settings": {
"location": "/data/es/backup",
"compress": true
}
}
PUT /_snapshot/fs_backup/snap-20251025?wait_for_completion=false
{
"indices": "logs-*,articles",
"ignore_unavailable": true,
"include_global_state": false
}
POST /_snapshot/fs_backup/snap-20251025/_restore
{
"indices": "articles",
"rename_pattern": "(.+)",
"rename_replacement": "restored_$1"
}

分片分配​

GET /_cluster/allocation/explain
{
"index": "logs-2025.10.25",
"shard": 0,
"primary": false
}
POST /_cluster/reroute
{
"commands": [
{
"allocate_replica": {
"index": "logs-2025.10.25",
"shard": 0,
"node": "es-node-2"
}
}
]
}

_cat 命令​

_cat 系列是纯文本表格,?v 带表头,?h= 选列,?s= 排序,?format=json 输出 JSON。完整清单用 GET /_cat 或 GET /_cat/help。

命令说明
_cat/health?v集群健康概览
_cat/master?v当前 master 节点
_cat/nodes?v节点角色、堆、CPU、负载、磁盘
_cat/nodeattrs?v节点自定义属性(机架、区域)
_cat/indices?v&s=store.size:desc索引列表,按大小排序
_cat/shards?v分片分布与状态
_cat/allocation?v每节点分片数、磁盘占用、剩余
_cat/segments?v段数量与大小
_cat/recovery?v&active_only=true正在恢复的分片与进度
_cat/thread_pool?v线程池活跃、队列、拒绝数
_cat/pending_tasks?v积压的集群任务
_cat/fielddata?v&bytes=gbfielddata 内存占用
_cat/aliases?v别名与索引的对应关系
_cat/templates?v索引模板列表
_cat/count?v文档总数
_cat/plugins?v已安装插件
_cat/health?v&h=status,node.total,shards只取几列
_cat/nodes?v&h=name,heap.percent,disk.avail,cpu,load_1m巡检常用列组合

索引与文档​

索引​

方法路径说明
PUT/{index}建索引(可带 settings 和 mappings)
GET/{index}查看索引定义
DELETE/{index}删除索引(数据不可恢复)
GET/{index}/_mapping查看 Mapping
PUT/{index}/_mapping新增字段(已有字段类型不能改)
GET/{index}/_settings查看索引设置
PUT/{index}/_settings修改副本数、refresh_interval 等
POST/{index}/_close关闭索引(不可读写,省资源)
POST/{index}/_open打开索引
POST/{index}/_refresh立即刷新,让写入可搜索
POST/{index}/_flush刷盘并清空 translog
POST/{index}/_forcemerge?max_num_segments=1合并段(只对只读索引做)
POST/{index}/_cache/clear?fielddata=true清 fielddata 缓存
POST/_reindex重建索引搬数据
POST/_aliases批量增删别名
GET/_index_template索引模板列表
PUT/_index_template/{name}创建/更新索引模板
PUT /articles/_settings
{
"index": {
"number_of_replicas": 1,
"refresh_interval": "5s"
}
}
PUT /articles/_mapping
{
"properties": {
"summary": { "type": "text" },
"updated_at": { "type": "date" }
}
}

文档​

方法路径说明
PUT/{index}/_doc/{id}指定 ID 写入(覆盖,幂等)
POST/{index}/_doc自动生成 ID 写入
POST/{index}/_create/{id}仅当文档不存在时创建
GET/{index}/_doc/{id}按 ID 实时读取
HEAD/{index}/_doc/{id}判断文档是否存在
POST/{index}/_update/{id}局部更新字段
DELETE/{index}/_doc/{id}删除文档
POST/_bulk批量写(NDJSON)
GET/_mget批量按 ID 读
GET/{index}/_count统计文档数
POST/{index}/_delete_by_query按条件批量删除(重操作)
POST/{index}/_update_by_query按条件批量更新(重操作)
POST /articles/_update/1
{
"doc": {
"status": "archived"
}
}
POST /_mget
{
"docs": [
{ "_index": "articles", "_id": "1" },
{ "_index": "articles", "_id": "2" }
]
}

bulk 请求格式​

{"index":{"_index":"articles","_id":"10"}}
{"title":"运维手册","status":"published"}
{"update":{"_index":"articles","_id":"10"}}
{"doc":{"status":"archived"}}
{"delete":{"_index":"articles","_id":"11"}}
curl -X POST "http://localhost:9200/_bulk" \
-H 'Content-Type: application/x-ndjson' \
--data-binary @actions.ndjson

查询与聚合​

常用查询​

场景写法说明
全文检索match对 text 字段分词后匹配
短语匹配match_phrase词序相邻
多字段检索multi_match可用 ^ 加权
精确匹配term / termskeyword 字段
范围rangegte / gt / lte / lt
组合boolmust / filter / should / must_not
存在性exists字段是否存在且有值
前缀prefixkeyword 字段前缀匹配
通配wildcard性能差,慎用
模糊fuzzy拼写容错
地理geo_distance按距离圈选
GET /articles/_search
{
"query": {
"bool": {
"must": [
{ "match": { "title": "Elasticsearch" } }
],
"filter": [
{ "term": { "status": "published" } },
{ "range": { "created_at": { "gte": "now-30d/d" } } }
],
"must_not": [
{ "term": { "tags": "deprecated" } }
]
}
},
"from": 0,
"size": 20,
"sort": [
{ "created_at": "desc" },
{ "_id": "asc" }
],
"_source": ["title", "created_at"],
"track_total_hits": true
}

深分页用 search_after:

GET /articles/_search
{
"size": 20,
"sort": [
{ "created_at": "desc" },
{ "_id": "asc" }
],
"search_after": ["2025-10-01T08:00:00", "abc123"]
}

聚合​

GET /articles/_search
{
"size": 0,
"aggs": {
"by_status": {
"terms": { "field": "status", "size": 10 },
"aggs": {
"avg_views": { "avg": { "field": "view_count" } }
}
},
"per_month": {
"date_histogram": {
"field": "created_at",
"calendar_interval": "month",
"format": "yyyy-MM"
}
}
}
}
聚合类型用途
terms按字段值分组,类似 group by
date_histogram按时间分桶
histogram按数值区间分桶
avg / sum / min / max数值统计
cardinality去重计数(近似)
percentiles分位数(P95、P99)
nested针对 nested 字段聚合
composite分页聚合,桶很多时用

一次多个查询​

{"index":"articles"}
{"query":{"match":{"title":"elasticsearch"}},"size":3}
{"index":"articles"}
{"query":{"term":{"status":"published"}},"size":3}
curl -X POST "http://localhost:9200/_msearch" \
-H 'Content-Type: application/x-ndjson' \
--data-binary @queries.ndjson

状态码与常见错误​

状态码错误含义与处理
200正常查询成功,注意 timed_out 字段
201已创建索引/文档创建成功
400mapper_parsing_exception字段类型与 Mapping 不符,改数据或 Mapping
400illegal_argument_exception参数写错,比如聚合字段是 text
404index_not_found_exception索引不存在,检查名字或先创建
404document_missing_exception文档不存在
409version_conflict_engine_exception并发写冲突,重试或加 ?version=
429es_rejected_execution_exception线程池队列满,退避重试 + 降并发
503unavailable_shards_exception主分片未分配,先看集群是否 red
# 一个带完整错误信息的查询示例
curl -s -X GET "http://localhost:9200/articles/_search?pretty" \
-H 'Content-Type: application/json' \
-d '
{
"query": { "term": { "title": "运维" } },
"size": 1
}
'

term 查 text 字段不会报错,只会「查不到」——这类静默失败最费时间,排查时先用 _mapping 确认字段类型。